﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;

namespace WebEFDemo
{
    /// <summary>
    /// ajaxupdate 的摘要说明
    /// </summary>
    public class ajaxupdate : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int id = Convert.ToInt32(context.Request.Form["ID"]);
            EFResult result = new EFResult();
            JavaScriptSerializer js = new JavaScriptSerializer();
            //1.实例化数据库访问上下文
            EFDemoUserListEntities db = new EFDemoUserListEntities();
            //2.根据ID查询实体
            var model = db.Student.FirstOrDefault(s => s.stuid == id);
            if (model == null)
            {
                result.IsSuccess = false;
                result.ErrorMessage = "该记录已删除！";
                context.Response.Write(js.Serialize(result));
            }
            //db.Student.Remove(model);
            //3.给实体标记为删除
            db.Entry<Student>(model).State = System.Data.Entity.EntityState.Deleted;
            //4.SaveChanges()
            if (db.SaveChanges() > 0)
            {
                result.IsSuccess = true;
                result.ErrorMessage = "删除成功！";
                context.Response.Write(js.Serialize(result));
            }
            else
            {
                result.IsSuccess = false;
                result.ErrorMessage = "删除失败";
                context.Response.Write(js.Serialize(result));
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}